package com.component.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.component.domain.MDocument;
import com.component.domain.MFilebase64;
import com.component.service.MDocumentService;
import com.component.mapper.MDocumentMapper;
import com.component.service.MFilebase64Service;
import com.system.security.SecurityUtil;
import com.system.security.domain.SelfUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 *
 */
@Service
public class MDocumentServiceImpl extends ServiceImpl<MDocumentMapper, MDocument>
    implements MDocumentService{

    @Autowired
    private MFilebase64Service mFilebase64Service;

    @Override
    public IPage<MDocument> page(String keyword, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<MDocument> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        IPage<MDocument> page=this.baseMapper.selectPage(new Page(pageNum,pageSize),lambdaQueryWrapper);
        return page;
    }

    @Override
    public MDocument save(MultipartFile file, String note) {
        SelfUserEntity userEntity=SecurityUtil.getSelfUserEntity();
        MFilebase64 mFilebase64=mFilebase64Service.uploadFile(file);
        MDocument mDocument=new MDocument();
        mDocument.setFiletye(file.getContentType());
        mDocument.setUserid(userEntity.getUserId().toString());
        mDocument.setFilename(file.getOriginalFilename());
        if(note!=null){
            mDocument.setNote(note);
        }
        mDocument.setFileid(mFilebase64.getId().toString());
        this.baseMapper.insert(mDocument);
        return mDocument;
    }


}




